import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import org.rsbot.event.events.MessageEvent;
import org.rsbot.event.listeners.MessageListener;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Bank;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Skills;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.util.Timer;
import org.rsbot.script.wrappers.RSArea;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;
import org.rsbot.script.wrappers.RSTilePath;

@ScriptManifest(authors={"LastCoder"}, keywords={"Smith, Auto"}, name="ArbiSmitherLite", version=2.0D, description="Start, all options are in GUI.")
public class AutoSmith extends Script
  implements MessageListener, PaintListener
{
  private static final AutoSmith.SMLoc[] LOCATIONS = { new AutoSmith.SMLoc("AL_KHARID", 11666, new RSArea(new RSTile(3275, 3175), new RSTile(3264, 3160)), new RSTile[] { new RSTile(3271, 3166), new RSTile(3273, 3166), new RSTile(3274, 3167), new RSTile(3274, 3169), new RSTile(3275, 3172), new RSTile(3276, 3174), new RSTile(3276, 3177), new RSTile(3277, 3178), new RSTile(3278, 3180), new RSTile(3280, 3182), new RSTile(3281, 3183), new RSTile(3281, 3185), new RSTile(3280, 3185), new RSTile(3278, 3185), new RSTile(3277, 3186), new RSTile(3276, 3186) }) };

  private static final Color COLOR_1 = new Color(0, 0, 0, 155);
  private static final Color COLOR_2 = new Color(0, 0, 0);
  private static final Color COLOR_3 = new Color(255, 255, 255);
  private static final BasicStroke STROKE = new BasicStroke(1.0F);
  private static final Font FONT_1 = new Font("Arial", 0, 17);
  private static final Font FONT_2 = new Font("Arial", 0, 9);
  private static final AutoSmith.Materials[] Materials = { new AutoSmith.Materials("Bronze Bars", 1, 436, 438, 14, 14), new AutoSmith.Materials("Iron Bars", 15, 440, -1, 28, -1), new AutoSmith.Materials("Silver Bars", 20, 442, -1, 28, -1), new AutoSmith.Materials("Steel Bars", 30, 440, 453, 9, 18), new AutoSmith.Materials("Gold Bars", 40, 444, -1, 28, -1), new AutoSmith.Materials("Mithril Bars", 50, 447, 453, 12, 14), new AutoSmith.Materials("Adamant Bars", 70, 449, 453, 4, 24), new AutoSmith.Materials("Rune Bars", 85, 451, 453, 3, 24) };
  private static AutoSmith.SMLoc location;
  private static AutoSmith.Materials MATERIAL;
  private long activity_time;
  private static boolean gui_on = false;
  private RSTilePath path;
  private RSTilePath path_back;
  private long startExp;
  private long startTime;
  private long expGained;
  private int expHour;
  private int barsCrafted;

  public boolean onStart()
  {
    new AutoSmith.Gui().setVisible(true);
    while (gui_on) {
      sleep(20);
    }
    if (location.equals(LOCATIONS[0]))
      log("We are smithing in Al-Kharid.");
    else if (location.equals(LOCATIONS[1])) {
      log("We are smithing in Varrock.");
    }
    this.path = this.walking.newTilePath(location.walk_path);
    this.path_back = this.walking.newTilePath(location.walk_path).reverse();
    this.startExp = this.skills.getCurrentExp(13);
    this.startTime = System.currentTimeMillis();
    return this.game.isLoggedIn();
  }

  private boolean inArea(RSArea paramRSArea) {
    return paramRSArea.contains(new RSTile[] { getMyPlayer().getLocation() });
  }

  private int distanceTo(int paramInt) {
    RSObject localRSObject = this.objects.getNearest(new int[] { paramInt });
    if (localRSObject != null) {
      return this.calc.distanceTo(localRSObject);
    }
    return -1;
  }

  private boolean busy() {
    return System.currentTimeMillis() - this.activity_time < 8000L;
  }

  private AutoSmith.state getState() {
    if (this.interfaces.get(905).isValid()) {
      log("Interface is valid.");
      return AutoSmith.state.INTERFACE;
    }if (inArea(location.bank_area)) {
      if (this.inventory.contains(MATERIAL.inv_id)) {
        if (MATERIAL.inv_second_id != -1) {
          if (this.inventory.contains(MATERIAL.inv_second_id)) {
            log("Inventory contains 1st and 2nd");
            return AutoSmith.state.WALK;
          }
          return AutoSmith.state.BANK;
        }

        return AutoSmith.state.WALK;
      }

      return AutoSmith.state.BANK;
    }
    if (distanceTo(location.furnace_id) < 5) {
      if (!busy()) {
        if (this.inventory.contains(MATERIAL.inv_id)) {
          return AutoSmith.state.SMITH;
        }
        return AutoSmith.state.WALK;
      }

      return AutoSmith.state.SLEEP;
    }

    return AutoSmith.state.WALK;
  }

  public int loop()
  {
    switch (AutoSmith.1.$SwitchMap$AutoSmith$state[getState().ordinal()])
    {
    case 1:
      int i;
      if (!this.bank.isOpen()) {
        this.bank.open();
        for (i = 0; (i < 100) && (!this.bank.isOpen()); i++)
          sleep(20);
      }
      else if (MATERIAL.inv_second_id != -1) {
        if (!this.inventory.contains(MATERIAL.inv_id)) {
          if (this.inventory.getCount() > 1) {
            this.bank.depositAll();
          }
          this.bank.withdraw(MATERIAL.inv_id, MATERIAL.primary);
          i = 0;
          for (; (i < 100) && (!this.inventory.contains(MATERIAL.inv_id)); i++)
            sleep(20);
        }
        if (this.inventory.contains(MATERIAL.inv_second_id)) break;
        this.bank.withdraw(MATERIAL.inv_second_id, MATERIAL.secondary);

        i = 0;
        for (; (i < 100) && (!this.inventory.contains(MATERIAL.inv_second_id)); i++)
          sleep(20);
      }
      else {
        if (this.inventory.contains(MATERIAL.inv_id)) break;
        if (this.inventory.getCount() > 1) {
          this.bank.depositAll();
        }
        this.bank.withdraw(MATERIAL.inv_id, MATERIAL.primary);
        i = 0;
        for (; (i < 100) && (!this.inventory.contains(MATERIAL.inv_id)); i++)
          sleep(20);
      }
      break;
    case 2:
      if (MATERIAL.inv_second_id != -1) {
        if ((this.inventory.contains(MATERIAL.inv_id)) && (this.inventory.contains(MATERIAL.inv_second_id)))
        {
          this.path.traverse();
          sleep(20);
        } else {
          this.path_back.traverse();
          sleep(20);
        }
      }
      else if (this.inventory.contains(MATERIAL.inv_id))
        this.path.traverse();
      else {
        this.path_back.traverse();
      }

      break;
    case 3:
      RSObject localRSObject = this.objects.getNearest(new int[] { location.furnace_id });
      if (localRSObject == null) break;
      if (!localRSObject.isOnScreen()) {
        this.camera.turnToObject(localRSObject);
        for (int j = 0; (j < 100) && (!localRSObject.isOnScreen()); j++)
          sleep(20);
      }
      else if (!this.inventory.isItemSelected()) {
        RSItem localRSItem = this.inventory.getItem(new int[] { MATERIAL.inv_id });
        if (localRSItem != null) {
          localRSItem.doAction("Use");
        }
        for (int k = 0; (k < 100) && (!this.inventory.isItemSelected()); k++)
          sleep(20);
      } else {
        localRSObject.doAction("Furnace");
        sleep(200); } break;
    case 4:
      this.interfaces.get(905).getComponent(14).doAction("All");
      break;
    case 5:
      sleep(20);
    }

    return random(600, 1200);
  }

  public void messageReceived(MessageEvent paramMessageEvent)
  {
    String str = paramMessageEvent.getMessage();
    if ((str.contains("You smelt")) || (str.contains("You"))) {
      this.activity_time = System.currentTimeMillis();
      this.barsCrafted += 1;
    }
  }

  public void onRepaint(Graphics paramGraphics)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    long l = System.currentTimeMillis() - this.startTime;
    String str = Timer.format(l);
    if ((this.skills.getCurrentExp(13) - this.startExp > 0L) && (this.startExp > 0L))
    {
      this.expGained = (this.skills.getCurrentExp(13) - this.startExp);
    }
    if ((this.expGained > 0L) && (l > 0L)) {
      this.expHour = (int)(3600L * this.expGained / l);
    }
    localGraphics2D.setColor(COLOR_1);
    localGraphics2D.fillRect(14, 350, 474, 99);
    localGraphics2D.setColor(COLOR_2);
    localGraphics2D.setStroke(STROKE);
    localGraphics2D.drawRect(14, 350, 474, 99);
    localGraphics2D.setFont(FONT_1);
    localGraphics2D.setColor(COLOR_3);
    localGraphics2D.drawString("AutoSmither", 209, 374);
    localGraphics2D.setFont(FONT_2);
    localGraphics2D.drawString("EXP/Hr: " + this.expHour, 18, 390);
    localGraphics2D.drawString("EXP Gained: " + this.expGained, 18, 400);
    localGraphics2D.drawString("Time Ran: " + str, 182, 390);
    localGraphics2D.drawString("Status: " + getState().toString(), 182, 400);
    localGraphics2D.drawString("Bars Crafted: " + this.barsCrafted, 395, 390);
  }
}